package com.example.javaeekeshe.utils.encryUtils.impl;

import com.example.javaeekeshe.utils.CharUtils;
import com.example.javaeekeshe.utils.encryUtils.IEncryption;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 加密
 * @author 化粪池堵塞的凶手
 */
@Service("Md5")
public class MD5EncryImpl implements IEncryption {
    public static final String KEY_MD5 = "MD5";

    /**
     * 加密
     * @param txt
     * @return
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String encry(String txt) {

        try {
            byte[] data = txt.getBytes(StandardCharsets.UTF_8);
            MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
            md5.update(data);
            return  CharUtils.bytes2Hex(md5.digest());
        }catch (Exception e){
            throw new RuntimeException();
        }

    }

    @Override
    public String dencry(String txt) {
        return null;
    }
}
